﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms
{
    public partial class frmDistribute : Form
    {
        public Boolean isAddNew = false;
        public Boolean isLoading = true;
        public int m_distributeID = -1;
        public string m_name = string.Empty;
        public int permission = 0;
        public frmDistribute()
        {
            InitializeComponent();
            this.isLoading = false;
            GetData();
            permission = CtrAdmin.GetPermission(Program.UserEntity.Permission, Config.Enum.Function.frmDistribute);

            if ((permission & (int)Config.Enum.Permission.Add) != (int)Config.Enum.Permission.Add)
            {
                tsbAdd.Enabled = false;                
            }

            if ((permission & (int)Config.Enum.Permission.Edit) != (int)Config.Enum.Permission.Edit)
            {
                tsbEdit.Enabled = false;                
            }

            if ((permission & (int)Config.Enum.Permission.Delete) != (int)Config.Enum.Permission.Delete)
            {
                tsbDelete.Enabled = false;                
            }
        }

        public void GetData()
        {
            listView1.Items.Clear();
            CtrBase ctrBase = new CtrBase();
            var objNcc = ctrBase.DistributeGetAll();
            int i = 1;
            foreach (var item in objNcc)
            {
                ListViewItem lstItem = new ListViewItem((i++).ToString());
                lstItem.SubItems.Add(item.DistributeName);
                lstItem.SubItems.Add(item.DistributePhone);
                lstItem.SubItems.Add(item.DistributeAddress);
                lstItem.SubItems.Add(item.StatusName);
                lstItem.Tag = item.DistributeID;
                listView1.Items.Add(lstItem);
            }
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            this.isAddNew = true;
            popupDistributeDetail popupDistributeDetail = new Forms.popupDistributeDetail(this);
            popupDistributeDetail.ShowDialog();
            popupDistributeDetail.Dispose();   
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.isLoading) return;
                this.isAddNew = false;
                this.m_distributeID = Convert.ToInt32(listView1.SelectedItems[0].Tag);
                this.m_name = listView1.SelectedItems[0].Text;
                popupDistributeDetail popupDistributeDetail = new popupDistributeDetail(this);
                popupDistributeDetail.ShowDialog();
                popupDistributeDetail.m_distributeID = this.m_distributeID;
            }
            catch (Exception loi)
            {
                MessageBox.Show(loi.Message.ToString());
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Có đồng ý xoá bản ghi này không?", "Thông báo...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            CtrBase ctrBase = new CtrBase();            
            var objBase = ctrBase.DistributeDelete(this.m_distributeID);
            if (objBase > 0)
            {
                CtrAdmin ctra = new CtrAdmin();
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 3, "Xóa nhà cung cấp \"" + this.m_name + "\" có mã \"" + objBase + "\"", "Quản lý nhà cung cấp");
                MessageBox.Show("Xóa nhà cung cấp thành công!", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.GetData();
            }
        }

        private void tsbRefresh_Click(object sender, EventArgs e)
        {
            this.GetData();
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
